/*
    Copyright (c) Guangzhou Data Control Network Technology Co., Ltd. All rights reserved.
    Licensed under the MIT License.
    contact:dcdev_founder@foxmail.com
*/
function elementClass() {
    elementClass.prototype.loadJs =
        function (url, callback) {
            script = document.createElement('script');
            script.type = "text/javascript";
            if (typeof (callback) != "undefined") {
                if (script.readyState) {
                    script.onreadystatechange = function () {
                        if (script.readyState == "loaded" || script.readyState == "complete") {
                            script.onreadystatechange = null;
                            callback();
                        }
                    }
                } else {
                    script.onload = function () {
                        callback();
                    }
                }
            }
            script.src = url;
            document.body.appendChild(script);
        };
    elementClass.prototype.insertElement = function (id, element, class_name = null, style = null) {
        _id = document.getElementById(id);
        if (!_id) {
            _element = document.createElement(element);
            _element.id = id;
            _element.name = id;
            _element.style.cssText = style;
            _element.className = class_name;
            var first = document.body.firstChild;//得到页面的菜单项1元素 
            document.body.insertBefore(_element, first)
            // document.body.appendChild(_element);
        }
    }
}
// 如果不存在则插入
if (!document.getElementById("_dc_qrcode_body_")) {
    // 插入div用于承载qrcode
    new elementClass().insertElement("_dc_qrcode_body_", "div", null,
        `width:100%;height:100%;margin:0;padding:0;position:absolute;top:0px;left:0px;display:flex;background: rgb(0,0,0,1);z-index:999;align-items:center;justify-content:center;`)
}

function popup_qrcode(data, opacity = 1, cb) {
    $("#_dc_qrcode_body_").css("opacity", opacity);
    window["_dc_qrcode_body_cb_"] = cb;

    // 生成二维码
    new elementClass().loadJs("https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js", function () {
        new elementClass().loadJs("https://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js", function () {
            scale = 1;
            if (data.scale) {
                scale = data.scale;
            }
            // 显示
            jQuery('#_dc_qrcode_body_').qrcode(data);
            $("#_dc_qrcode_body_").children("table").css("border-width", "3px").css("border-style", "solid").css("border-color", "white").css("transform", `scale(${scale})`);
            // 点击关闭
            $("#_dc_qrcode_body_").click(function () {
                if (window["_dc_qrcode_body_cb_"])
                    window["_dc_qrcode_body_cb_"]();
                $(this).remove();

            })
        });
    });
}

